﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Check_Printing_Solution
{
    public partial class Log : Form
    {
        PrintManager manager = new PrintManager();

        public Log()
        {
            InitializeComponent();
            dteStart.Value = DateTime.Now;
            dteEnd.Value = DateTime.Now;
        }

        private void Log_Load( object sender, EventArgs e )
        {
            dataGridView1.DataSource = manager.GetLogs();
            dataGridView1.Columns[ "Id" ].Visible = false;

            cmbBanks.DataSource = manager.GetAllBanks();
            cmbBanks.DisplayMember = "BranchPrefix";
            cmbBanks.ValueMember = "Id";

            GetLogs();
        }

        private void dteStart_ValueChanged( object sender, EventArgs e )
        {
            GetLogs();
        }

        private void dteEnd_ValueChanged( object sender, EventArgs e )
        {
            GetLogs();
        }

        private void GetLogs()
        {
            dgPrintLog.DataSource = ReturnLogs();

            dgPrintLog.Columns[ "Id" ].Visible = false;
            dgPrintLog.Columns[ "Comment" ].Visible = false;
            dgPrintLog.Columns[ "DatePrinted" ].Visible = false;
            dgPrintLog.Columns[ "LastQuantity" ].Visible = false;
            dgPrintLog.Columns[ "LastChequeNumber" ].Visible = false;
            dgPrintLog.Columns[ "Id" ].Visible = false;
        }

        private void btnPrint_Click( object sender, EventArgs e )
        {
            if ( saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK )
            {
                List<JobLog> logs = ReturnLogs();

                string doctitle = "<h2>Trinidad DataForms Ltd.</h2><h3>Report Period from " + dteStart.Value.ToShortDateString() + " to " + dteEnd.Value.ToShortDateString() + "</h3>";
                string tablestart = "<table>";
                string header = "<tr><td><strong>Cheque Type</strong></td><td><strong>Bank</strong></td><td><strong>Account Number</strong></td><td><strong>Branch</strong></td><td><strong>Number Books</strong></td></tr>";
                StringBuilder htmlbody = new StringBuilder();
                foreach ( JobLog log in logs )
                {
                    htmlbody.Append( "<td>" );
                    htmlbody.Append( log.ChequeTypeId );
                    htmlbody.Append( "</td>" );

                    htmlbody.Append( "<td>" );
                    htmlbody.Append( log.Bank );
                    htmlbody.Append( "</td>" );

                    htmlbody.Append( "<td>" );
                    htmlbody.Append( log.CustomerId );
                    htmlbody.Append( "</td>" );

                    htmlbody.Append( "<td>" );
                    htmlbody.Append( log.Branch );
                    htmlbody.Append( "</td>" );

                    htmlbody.Append( "<td>" );
                    htmlbody.Append( log.NumberBooks );
                    htmlbody.Append( "</td>" );
                    htmlbody.Append( "</tr>" );
                }

                string tableend = "</table>";

                string htmlreport = doctitle + tablestart + header + htmlbody.ToString() + tableend;

                using ( FileStream fs = File.Create( saveFileDialog1.FileName ) )
                {
                    // Add some text to file
                    Byte[] title = new UTF8Encoding( true ).GetBytes( htmlreport );
                    fs.Write( title, 0, title.Length );
                }
                //MessageBox.Show( "Report file exported.", "Report", MessageBoxButtons.OK, MessageBoxIcon.Information );

                Process.Start( "IExplore.exe", saveFileDialog1.FileName );
            }
        }

        private List<JobLog> ReturnLogs()
        {
            if ( rdoSelectedBank.Checked )
            {
                return manager.GetAllJobLogs( dteStart.Value, dteEnd.Value, cmbBanks.Text, chkShowReprints.Checked );
            }
            else
            {
                return manager.GetAllJobLogs( dteStart.Value, dteEnd.Value, chkShowReprints.Checked );
            }
        }

        private void rdoAllBanks_CheckedChanged( object sender, EventArgs e )
        {
            if ( rdoAllBanks.Checked )
            {
                cmbBanks.Enabled = false;
                GetLogs();
            }
        }

        private void rdoSelectedBank_CheckedChanged( object sender, EventArgs e )
        {
            if ( rdoSelectedBank.Checked )
            {
                cmbBanks.Enabled = true;
                GetLogs();
            }
        }

        private void cmbBanks_SelectedIndexChanged( object sender, EventArgs e )
        {
            GetLogs();
        }

        private void button1_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void chkShowReprints_CheckedChanged( object sender, EventArgs e )
        {
            GetLogs();
        }

        private void contextMenuStrip1_Click( object sender, EventArgs e )
        {

        }

        private void toolStripMenuItem1_Click( object sender, EventArgs e )
        {
            //Flag as reprint
            manager.FlagReprint( true, Convert.ToInt32( dgPrintLog.SelectedRows[ 0 ].Cells[ 0 ].Value.ToString() ) );
            dgPrintLog.SelectedRows[ 0 ].DefaultCellStyle.BackColor = Color.Yellow;
        }

        private void toolStripMenuItem2_Click( object sender, EventArgs e )
        {
            //Unflag as repring
            manager.FlagReprint( false, Convert.ToInt32( dgPrintLog.SelectedRows[ 0 ].Cells[ 0 ].Value.ToString() ) );
            dgPrintLog.SelectedRows[ 0 ].DefaultCellStyle.BackColor = Color.Yellow;
        }
    }
}
